package pso;

public class Point {
	private double[] x;
	private int n; // dimension
	
	public Point(int dimension){
		n = dimension;
		x = new double[n];
	}
	
	public Point(double[] coordinates){
		x = coordinates;
		n = x.length;
	}
	
	public Point(int dimension, double value) {
		n = dimension;
		x = new double[n];
		for(int i = 0; i < n; i++){
			x[i] = value;
		}
	}

	public double[] getCoordinates(){
		return x.clone();
	}
	
	public int getDimension(){
		return n;
	}
	
	public Point add(Point p){
		double c1[] = getCoordinates();
		double c2[] = p.getCoordinates();
		double res[] = new double[c1.length];
		for(int i = 0; i < c1.length; ++i){
			res[i] = c1[i] + c2[i];
		}
		return new Point(res);
	}
	
	public Point subtract(Point p){
		double c1[] = getCoordinates();
		double c2[] = p.getCoordinates();
		double res[] = new double[c1.length];
		for(int i = 0; i < c1.length; ++i){
			res[i] = c1[i] - c2[i];
		}
		return new Point(res);
	}
	
	public Point negate(){
		return new Point(getDimension()).subtract(this);
	}

	
	public Point scale(double scaleFactor){
		double c[] = getCoordinates();
		for(int i = 0; i < c.length; ++i){
			c[i] *= scaleFactor;
		}
		return new Point(c);
	}
	
	public double getCoordinate(int axis){
		return x[axis];
	}
	
	public void setCoordinate(int axis, double value){
		x[axis] = value;
	}

	
	public String toString(){
		String pString = "";
		if(n > 0)
			pString += x[0];
		for(int i=1; i<n; i++)
		{
			pString += " " + x[i];
		}
		pString += " \n";
		return pString;
	}



}
